
/* ____  _____             ____    ____    _                                 ______    _________   ____  _    *
* |_   \|_   _|           |_   \  /   _|  (_)                              .' ____ \  |  _   _  | |_   _|     *
*   |   \ | |    __   _     |   \/   |    __    .---.   _ .--.    .--.     | (___ \_| |_/ | | \_|   | |       *
*   | |\ \| |   [  | | |    | |\  /| |   [  |  / /'`\] [ `/'`\] / .'`\ \    _.____`.      | |       | |   _   *
*  _| |_\   |_   | \_/ |,  _| |_\/_| |_   | |  | \__.   | |     | \__. |   | \____) |    _| |_     _| |__/ |  *
* |_____|\____|  '.__.'_/ |_____||_____| [___] '.___.' [___]     '.__.'     \______.'   |_____|   |________|  *
*                                                                                                             *
* @file     adc_mux_test_runtime.c                                                                            *
* @version  V1.00                                                                                             *
* $Date: 21/03/10 7:27p $                                                                                     *
* @brief    IEC60730 ADC/MUX Test                                                                             *
* @note                                                                                                       *
* SPDX-License-Identifier: Apache-2.0                                                                         *
* @copyright (C) 2016-2020 Nuvoton Technology Corp. All rights reserved.                                      *
***************************************************************************************************************/
#include <stdio.h>
#include "nuc980.h"
#include "adc.h"
#include "IEC60730_CONTROL_PARAM.h"
#include "IEC60730_SAFETY_TEST_LIB.h"

volatile static int s_u32NormalComplete=0;
volatile static int s_u32adcData=0, s_u32vrefData;
volatile static int s_u32adcChBusy=0, s_u32muxChBusy;

static INT32 NormalConvCallback(UINT32 u32status, UINT32 u32userData)
{
    /* Converssion coplete */
    s_u32NormalComplete=1;
    s_u32adcData = u32status;
    return 0;
}

void IEC60730_ADC_MUX_Test_RunTime_Init(void)
{
    /* Set MUX channel to internal pull-high */
    outpw(REG_CLK_HCLKEN, inpw(REG_CLK_HCLKEN)|(1<<11)); //Enable GPIO engine
    GPIO_SetMode(ADC_PORT, BIT0<<MUX_CHANNEL, GPIO_MODE_INPUT);
    GPIO_SetPullCtl(ADC_PORT, BIT0<<MUX_CHANNEL, GPIO_PUSEL_PULL_UP);

    /* Open ADC */
    adcOpen();

    /* Enable Bandgap power */
    adcIoctl(VBPOWER_ON, NULL, NULL);

    /* Enable Normal AD Conversion */
    adcIoctl(NAC_ON,(UINT32)NormalConvCallback,0);

    /* Init flag */
    s_u32adcChBusy = 0;
    s_u32muxChBusy = 0;
}

uint8_t IEC60730_ADC_Test_RunTime(void)
{
    /* Check whether ADC is not busy or not */
    if(s_u32muxChBusy == 0)
    {
        if (!s_u32NormalComplete)
        {
            adcChangeChannel(VREFF_CHANNEL << ADC_CONF_CHSEL_Pos);
            adcIoctl(START_MST, NULL, NULL);
            s_u32adcChBusy = 1;
        }
        else
        {
            s_u32NormalComplete = 0;
            s_u32vrefData = s_u32adcData;
            s_u32adcChBusy = 0;
            if((s_u32adcData<VREFF_H) && (s_u32adcData>VREFF_L))        //For SW test breakpoint
                return TEST_PASS;
            else
                return TEST_FAIL;
        }
    }
    return TEST_PASS;
}

uint8_t IEC60730_MUX_Test_RunTime(void)
{
    /* Check whether ADC is not busy or not */
    if(s_u32adcChBusy == 0)
    {
        if (!s_u32NormalComplete)
        {
            adcChangeChannel(MUX_CHANNEL << ADC_CONF_CHSEL_Pos);
            adcIoctl(START_MST, NULL, NULL);
            s_u32muxChBusy = 1;
        }
        else
        {
            s_u32NormalComplete = 0;
            s_u32muxChBusy = 0;
            if(s_u32adcData != s_u32vrefData)       //For SW test breakpoint
                return TEST_PASS;
            else
                return TEST_FAIL;
        }
    }
    return TEST_PASS;
}


/*** (C) COPYRIGHT 2013 Nuvoton Technology Corp. ***/
